﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using SkyShell.Model;
using Coronox.SkyShell;
using System.IO;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Threading;

namespace SkyShell.ViewModel
{
    public class SyncViewModel : ViewModelBase
    {
        private string _targetPath;
        public string TargetPath
        {
            get
            {
                return _targetPath;
            }
            set
            {
                _targetPath = value;
                RaisePropertyChanged("TargetPath");
            }
        }

        private string _sourcePath;
        public string SourcePath
        {
            get
            {
                return _sourcePath;
            }
            set
            {
                _sourcePath = value;
                RaisePropertyChanged("SourcePath");
            }
        }


        private bool _validName;
        private string _alternativeName;
        public string AlternativeName
        {
            get
            {
                return _alternativeName;
            }
            set
            {
                _validName = false;
                string newPath = Path.Combine(Sky.GetSkyDriveFolder(), value);
                if (Directory.Exists(newPath))
                {
                    _createSideBySideFolder.RaiseCanExecuteChanged();
                    throw new ArgumentException("A folder with this name already exits");
                }
                int indx = value.IndexOfAny(Path.GetInvalidFileNameChars());
                if (indx > -1)
                {
                    _createSideBySideFolder.RaiseCanExecuteChanged();
                    throw new ArgumentException("Invalid character in folder name.");

                }
                _validName = true;
                _alternativeName = value;
                RaisePropertyChanged("AlternativeName");
                _createSideBySideFolder.RaiseCanExecuteChanged();
            }
        }


        public SyncViewModel()
        {

            _sideBySide = new RelayCommand(ExecSideBySide, CanSideBySide);
            _merge = new RelayCommand(ExecMerge, CanMerge);
            _createSideBySideFolder = new RelayCommand(ExecCreateSideBySideFolder, CanCreateSideBySideFolder);

            //MessengerInstance.Register<NotificationMessage<Tuple<string, string>>>(this, message =>
            //    {
            if (Environment.GetCommandLineArgs().Length > 3)
            {
                SourcePath = Environment.GetCommandLineArgs()[2];
                TargetPath = Environment.GetCommandLineArgs()[3];
                if (Environment.GetCommandLineArgs()[1] == "/sync")
                {

                    _alternativeName = Sky.SuggestAlternativeName(TargetPath, false);
                    _validName = true;
                }

                if (Environment.GetCommandLineArgs()[1] == "/localsync")
                {

                    _alternativeName = Sky.SuggestAlternativeName(SourcePath, true);
                    _validName = true;
                }
            }
            //    });

            //MessengerInstance.Send<NotificationMessage>(new NotificationMessage("request_parameter"));
        }


        private RelayCommand _sideBySide;
        public RelayCommand SideBySide
        {
            get
            {
                return _sideBySide;
            }
        }

        /// <summary>
        /// Checks whether the SideBySide command is executable
        /// </summary>
        private bool CanSideBySide()
        {
            return true;
        }

        /// <summary>
        /// Executes the SideBySide command 
        /// </summary>
        private void ExecSideBySide()
        {

            MessengerInstance.Send<NotificationMessage<NavigationTargets>>(new NotificationMessage<NavigationTargets>(NavigationTargets.SideBySideNaming, ""));

        }



        private RelayCommand _merge;
        public RelayCommand Merge
        {
            get
            {
                return _merge;
            }
        }

        /// <summary>
        /// Checks whether the Merge command is executable
        /// </summary>
        private bool CanMerge()
        {
            return true;
        }

        /// <summary>
        /// Executes the Merge command 
        /// </summary>
        private void ExecMerge()
        {
            string cantMergeReasom;
            if (Sky.CanMergeFolder(TargetPath, out cantMergeReasom) == false)
            {
                System.Windows.Forms.MessageBox.Show(cantMergeReasom, "Error", System.Windows.Forms.MessageBoxButtons.OK);
                return;
            }

            App.Current.MainWindow.Hide();
            Task t = new Task(() =>
             {
                 Sky.SyncFolder(_sourcePath, null, true);
                 DispatcherHelper.CheckBeginInvokeOnUI(() => App.Current.Shutdown());
             });
            t.Start();
        }



        private RelayCommand _createSideBySideFolder;
        public RelayCommand CreateSideBySideFolder
        {
            get
            {
                return _createSideBySideFolder;
            }
        }

        /// <summary>
        /// Checks whether the CreateSideBySideFolder command is executable
        /// </summary>
        private bool CanCreateSideBySideFolder()
        {
            return _validName;
        }

        /// <summary>
        /// Executes the CreateSideBySideFolder command 
        /// </summary>
        private void ExecCreateSideBySideFolder()
        {
            App.Current.MainWindow.Hide();
            Task t = new Task(() =>
          {
              Sky.SyncFolder(_sourcePath, _alternativeName, false);
              DispatcherHelper.CheckBeginInvokeOnUI(() => App.Current.Shutdown());
          });
            t.Start();
        }


    }
}
